﻿using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.MaterialsCommand.QueryMaterialsCommand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.MaterialsCommandCommand.QueryMaterialsCommandHandler
{
    /// <summary>
    /// 点击分类名进行查询命令处理方法
    /// </summary>
    public class MaterialCategoryByIdQueryCommandHandler : IRequestHandler<MaterialCategoryByIdQueryCommand, APIResult<List<MaterialCategoryModel>>>
    {
        private readonly IBaseRepository<MaterialCategoryModel> mRepo;

        public MaterialCategoryByIdQueryCommandHandler(IBaseRepository<MaterialCategoryModel>mRepo)
        {
            this.mRepo = mRepo;
        }
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public async Task<APIResult<List<MaterialCategoryModel>>> Handle(MaterialCategoryByIdQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<MaterialCategoryModel>> result = new APIResult<List<MaterialCategoryModel>>();
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.查询成功.ToString();
            var list = await mRepo.GetAll();
            if(request.id!=null)
            {
                list = list.Where(x=>x.id==request.id); 
            }
            result.data = list.ToList();
            return await Task.FromResult(result);   

        }
    }


}
